﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace Event_SG_EWDT
{
    public partial class dummy_test_multiview : System.Web.UI.Page
    {
        MultiView mv;

        protected void Page_PreInit()
        {
            #region fake sql data
                //Get list of events....
                ArrayList a = new ArrayList();
                a.Add(new string[] { "0001", "h1", "d1" });
                a.Add(new string[] { "0002", "h2", "d2" });
                a.Add(new string[] { "0003", "h3", "d3" });
                a.Add(new string[] { "0001", "h1", "d1" });
                a.Add(new string[] { "0002", "h2", "d2" });
                a.Add(new string[] { "0003", "h3", "d3" });
                a.Add(new string[] { "0001", "h1", "d1" });
                a.Add(new string[] { "0002", "h2", "d2" });
                a.Add(new string[] { "0003", "h3", "d3" });
                a.Add(new string[] { "0001", "h1", "d1" });
                a.Add(new string[] { "0002", "h2", "d2" });
                a.Add(new string[] { "0003", "h3", "d3" });
                a.Add(new string[] { "0001", "h1", "d1" });
                a.Add(new string[] { "0002", "h2", "d2" });
                a.Add(new string[] { "0003", "h3", "d3" });
                a.Add(new string[] { "0001", "h1", "d1" });
                a.Add(new string[] { "0002", "h2", "d2" });
                a.Add(new string[] { "0003", "h3", "d3" });
                a.Add(new string[] { "0001", "h1", "d1" });
                a.Add(new string[] { "0002", "h2", "d2" });
                a.Add(new string[] { "0003", "h3", "d3" });
            #endregion

            Queue<Control> ctrlList = new Queue<Control>();

            foreach (object o in a)
            {
                string[] s = (string[])o;

                ctrlList.Enqueue(CreateCustomControl(s[0], s[1], s[2]));
            }


            //Number of control per page = 5;
            //Total controls / 5...

            //1 is height of 150;

            mv = (MultiView)MultiPageContainer1.FindControl("mvEvents");

            int numCtrlToAdd = ctrlList.Count;

            int viewadded = 0;

            while (numCtrlToAdd > 0)
            {
                View v = new View();

                if (ctrlList.Count >= 5)
                {
                    for (int i = 0; i < 5; i++)
                    {
                        v.Controls.Add(ctrlList.Dequeue());
                    }
                }
                else
                {
                    int leftover = ctrlList.Count;

                    for (int i = 0; i < leftover; i++)
                    {
                        v.Controls.Add(ctrlList.Dequeue());
                    }

                    mv.Controls.Add(v);

                    break;
                }

                mv.Controls.Add(v);

                viewadded++;
            }

            int debug2 = mv.Views.Count;

            System.Diagnostics.Debug.WriteLine(viewadded + " added");

            DropDownList ddl = (DropDownList)MultiPageContainer1.FindControl("ddlPage");

            for (int i = 1; i <= mv.Views.Count; i++)
            {
                ddl.Items.Add(new ListItem(i.ToString()));
            }

            ddl.SelectedIndex = 0;
            mv.ActiveViewIndex = 0;

            ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);

            ddl.AutoPostBack = true;
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            // if (!IsPostBack)
        }

        void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
           DropDownList ddl = (DropDownList)sender;

            int newIndex = Convert.ToInt32(ddl.SelectedValue) - 1;

            mv.ActiveViewIndex = newIndex;
            ddl.SelectedIndex = newIndex;
        }

        Control CreateCustomControl(string eventid, string eventtitle, string desc)
        {
            Control c = LoadControl("~/CustomUserControl.ascx");

            HyperLink hyperlinkEvent = (HyperLink)c.FindControl("hyperlinkEvent");
            TextBox tbxUserControl = (TextBox)c.FindControl("tbxUserControl");
            Image imgUserControl = (Image)c.FindControl("imgUserControl");

            hyperlinkEvent.Text = eventtitle;
            hyperlinkEvent.NavigateUrl += String.Format("~/ViewEvent.aspx?eventid={0}", eventid);

            tbxUserControl.Text = desc;

            //************************************************ NEED TO UPDATE
            imgUserControl.ImageUrl = "~/images/events/" + eventid + "/1.jpg";

            return c;
        }


        }
 }